package top.xtcoder.jdcbase.base.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.beetl.sql.core.SQLManager;
import org.nutz.dao.Dao;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import top.xtcoder.jdcbase.base.core.Resp;
import top.xtcoder.jdcbase.base.redis.RedisCache;
import top.xtcoder.jdcbase.base.service.UploadService;

@Api(tags = "PR主页模块")
@RestController
@RequestMapping("/jdcbase/prbase")
public class PrBaseIndexController {
    @Value("${spring.profiles.active:dev}")
    private String profile;

    @Autowired
    private SQLManager sqlManager;

    @Autowired
    private Dao dao;

    @Value("${client.server.inteval.time:10}")
    private long clientServerIntevalTime;

    @ApiOperation("首页-欢迎页面")
    @GetMapping("/")
    public Resp<NutMap> index() {
    	NutMap map = NutMap.NEW();
    	map.addv("version", "2.7.5_0.0.1");
        return Resp.OK("欢迎使用 Project Base 基础模块", map);
    }
    
    @ApiOperation("检查客户端时间")
    @GetMapping("/checkTime")
    public Resp<Long> checkTime(
    	@ApiParam("客户端时间") 
    	@RequestParam(name="clientTime", defaultValue = "0") long clientTime) {
    	long time = System.currentTimeMillis();
    	long s = Math.abs(time - clientTime);
    	if(s < clientServerIntevalTime * 1000) {
    		return Resp.OK("成功", time);
    	}else {
    		return Resp.ERROR("客户端时间与服务器相差太大", s / 1000);
    	}
    }

    @ApiOperation("读取jar包同级目录的txt文件")
    @GetMapping("/{txtfile:\\w+}.txt")
    public String getTxtFile(@ApiParam("文件名") @PathVariable String txtfile
            , HttpServletRequest request, HttpServletResponse response) {
        txtfile = txtfile + ".txt";
        response.reset();
        response.setContentType("text/plain");
        response.setHeader("Content-Type", "text/plain;charset=utf-8");
        ApplicationHome jarHome = new ApplicationHome(getClass());
        File jarF = jarHome.getSource();
        String fileName = jarF.getParentFile().toString() + File.separatorChar + txtfile;
        File file = new File(fileName);
        if (!file.exists()) {
            return "welcome";
        }
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }

    @ApiOperation("读取jar包同级目录的html文件")
    @GetMapping("/verify_{txtfile:\\w+}.html")
    public String getHtmlFile(@ApiParam("文件名") @PathVariable String txtfile) {
        txtfile = "verify_" + txtfile + ".html";
        ApplicationHome jarHome = new ApplicationHome(getClass());
        File jarF = jarHome.getSource();
        String fileName = jarF.getParentFile().toString() + File.separatorChar + txtfile;
        File file = new File(fileName);
        if (!file.exists()) {
            return "welcome";
        }
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }
}
